package com.smritimap.demo;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

public class DrawableLoadActivity extends AppCompatActivity {

    private static final String TAG = DrawableLoadActivity.class.getSimpleName();
    private ConstraintLayout mMain;
    private ImageView mIvImageDrawable;
    private ImageView mIvDrawable;

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, DrawableLoadActivity.class);
        context.startActivity(intent);
    }

    String url = "https://img0.baidu.com/it/u=2316924685,3028247922&fm=253&fmt=auto&app=138&f=PNG?w=500&h=500";
    private final static String URL_DRAWABLE
            = "https://img0.baidu.com/it/u=838034426,3406054195&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=494";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_drawable_load);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        initView();
    }

    private void initView() {
        mMain = (ConstraintLayout) findViewById(R.id.main);
        mIvImageDrawable = (ImageView) findViewById(R.id.ivImageDrawable);

        /*Glide.with(this)
                .applyDefaultRequestOptions(new RequestOptions().placeholder(R.mipmap.ic_defalut_star))
                .load(url).into(mIvImageDrawable);*/
        mIvDrawable = (ImageView) findViewById(R.id.ivDrawable);

        Glide.with(this)
                .asDrawable()
                .load(url)
                .into(new CustomViewTarget<ImageView, Drawable>(mIvDrawable) {
                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        Log.e(TAG, "onLoadFailed: ");
                    }

                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        Log.e(TAG, "onResourceReady: ");
                        view.setImageDrawable(resource);
                    }


                    @Override
                    protected void onResourceCleared(@Nullable Drawable placeholder) {
                        Log.e(TAG, "onResourceCleared: ");
                    }

                });
    }
}